package gacha

type Message struct {
	Code    int                `json:"code"` //0：结果，-1：错误，1：info
	Message string             `json:"message"`
	Data    map[string][]GaCha `json:"data"`
}

type Ch struct {
	Ch chan Message
}

func NewCh() *Ch {
	return &Ch{Ch: make(chan Message)}
}

func BuildMessage(code int, message string, data map[string][]GaCha) *Message {
	return &Message{
		Code:    code,
		Message: message,
		Data:    data,
	}
}

func ErrorMessage(message string) *Message {
	return BuildMessage(-1, message, nil)
}

func InfoMessage(message string) *Message {
	return BuildMessage(1, message, nil)
}

func (c *Ch) send(message *Message) {
	c.Ch <- *message
}

func (c *Ch) close() {
	close(c.Ch)
}

func (c *Ch) sendError(message string) {
	c.send(ErrorMessage(message))
}

func (c *Ch) sendData(data map[string][]GaCha) {
	c.send(BuildMessage(0, ``, data))
}

func (c *Ch) sendMessage(message string) {
	c.send(InfoMessage(message))
}
